const ResultVo = require('../utils/ResultVo')
const CategoryService = require('../service/CategoryService')

class CategoryController {
  async create(req, res, next) {
    const { categoryName } = req.body
    const result = await CategoryService.create({ categoryName }).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async categoryList(req, res, next) {
    const { page, size } = req.query
    const result = await CategoryService.findAll(Number(page, Number(size))).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async categoryEdit(req, res, next) {
    const { id, categoryName } = req.body
    await CategoryService.update(id, { categoryName }).catch(err => next(err))
    res.json(ResultVo.successNull())
  }

  async categoryRemove(req, res, next) {
    const { id } = req.params;
    await CategoryService.remove(id).catch(err => next(err))
    res.json(ResultVo.successNull())
  }
}

module.exports = new CategoryController()